/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.memory.criteria.field;

import java.util.List;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;
import com.genia.toolbox.persistence.exception.AggregateException;
import com.genia.toolbox.persistence.exception.FieldDefinitionException;

/**
 * the base of the implementation of <code>Field</code> for the memory
 * implementation of the persistence API.
 */
public abstract class AbstractSimpleMemoryField
    extends AbstractMemoryField
{

  /**
   * Return the value of this field for the given line of result.
   * 
   * @param line
   *          the lines of result we compute the field on
   * @return the value of this field for the given line of result
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   * @see com.genia.toolbox.persistence.memory.criteria.field.AbstractMemoryField#getSimpleValue(com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult)
   */
  @Override
  public Object getSimpleValue(final BasicCriteriaResult<?> line)
      throws FieldDefinitionException
  {
    return getValue(line);
  }



  /**
   * Return the value of this field for the given lines of result.
   * 
   * @param lines
   *          the lines of result we compute the field on
   * @return the value of this field for the given line of result
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   */
  @Override
  public Object getValue(final List<BasicCriteriaResult<?>> lines)
      throws FieldDefinitionException, AggregateException
  {
    assert !lines.isEmpty() : "getValue() should never be called with an empty list of values";
    final Object[] results = new Object[lines.size()];
    int index = 0;
    for (final BasicCriteriaResult<?> line : lines) {
      results[index++] = getValue(line);
    }
    for (index = 1; index < results.length; index++) {
      if (!isSame(results[0], results[index])) {
        throw new AggregateException();
      }
    }
    return results[0];
  }



  /**
   * Compare two objects. Return <code>true</code> if the 2 objects are
   * <code>null</code> or are <code>equals</code>.
   * 
   * @param object1
   *          the first operand
   * @param object2
   *          the second operand
   * @return <code>true</code> if the 2 objects are <code>null</code> or are
   *         <code>equals</code>
   */
  private boolean isSame(final Object object1, final Object object2)
  {
    if (object1 == null) {
      return (object2 == null);
    }
    return object1.equals(object2);
  }



  /**
   * Return the value of this field for the given line of result.
   * 
   * @param line
   *          the lines of result we compute the field on
   * @return the value of this field for the given line of result
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   */
  public abstract Object getValue(BasicCriteriaResult<?> line)
      throws FieldDefinitionException;
}
